package com.efei.jdbc.dynamic.query.element;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.efei.jdbc.dynamic.query.entity.SysQueryEntity;
import com.efei.jdbc.sql.SqlSelect;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import lombok.Data;

@Data
public class QueryElement {
    private Long id;

    private String name;

    private String columStr; //查询列配置信息

    private String select;

    private String from;

    private String where;

    private String group;

    private String order;

    private String having;

    private List<QueryFilterElement> filters = new ArrayList<QueryFilterElement>();; // 局部过滤器

    private List<QueryFilterElement> rowFilters = new ArrayList<QueryFilterElement>(); //行过滤器

    private Map<String,List<QueryFilterElement>> columFilterMap = new HashMap<String, List<QueryFilterElement>>(); //列过滤器

    private Long glSearchId;

    private JSONArray cloumJson;

    public QueryElement(SysQueryEntity e) {

        this.columStr = e.getInfoColum();
        this.glSearchId = e.getGlSearchId();

        this.from = e.getInfoFrom();
        this.where = e.getInfoWhere();
        this.group = e.getInfoGroup();
        this.order = e.getInfoOrder();
        this.having = e.getInfoHaving();

        //this.filters = JSON.parseArray(e.getFilter(), String.class);
        List<QueryFilterElement> listFilters = JSON.parseArray(e.getFilter(), QueryFilterElement.class);
        if(listFilters==null) {
            listFilters =  new ArrayList<QueryFilterElement>();
        }
        for (QueryFilterElement filterInfo : listFilters) {
            String type = filterInfo.getType();
            if(StringUtils.equalsAnyIgnoreCase(QueryFilterElement.FILTER_TYPE_ROW, type)) {
                rowFilters.add(filterInfo);
            } else {
                filters.add(filterInfo);
            }
        }
        paseCloumJson();
    }

    /**
     * 获取 SQL 查询列信息
     * @return
     */
    private  void paseCloumJson() {
        StringBuilder sbSelect = new StringBuilder();
        try {
            cloumJson =JSON.parseArray(columStr);
        } catch (Exception e) {
            // 不是json 格式当作字符处理
            // sbSelect.append(columStr);
            this.select = columStr;
            return;
        }
        for(int i=0;i<cloumJson.size();i++) {
            JSONObject cloumJo = cloumJson.getJSONObject(i);
            String name = cloumJo.getString("name"); // 列名
            int type = cloumJo.getIntValue("type"); // 列类型，0：数据位 1：虚拟位置
            if(type!=0) {
                continue;
            }
            sbSelect.append(cloumJo.getString("filed"));
            if((i+1) < cloumJson.size()) {
                sbSelect.append(",");
            }
            // 列过滤器
            List<QueryFilterElement> filters = JSON.parseArray(cloumJo.getString("filter"), QueryFilterElement.class);

            if(!CollectionUtils.isEmpty(filters)) {
                columFilterMap.put(name, filters);
            }
        }
        this.select = sbSelect.toString();

    }

    /**
     * 获取动态SQL
     * @return
     */
    public SqlSelect getDynamicSql() {
        SqlSelect sql = SqlSelect.select();
        sql.select(select).from(from).where(where).group(group).orderBy(order);
        return sql;
    }
}
